/**
 * 
 */
package org.jenkon.core;

import java.util.HashMap;
import java.util.Map;

import org.jenkon.core.converter.BeanNameConverter;

/**
 * @author Jens Konopka
 * 
 */
abstract class AbstractApplicationContext implements ApplicationContext {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3424256901109599873L;

	private final BeanNameConverter beanNameConverter = new BeanNameConverter();
	private final Map<String, Object> beanMap = new HashMap<String, Object>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.core.ApplicationContext#addBean(java.lang.Object)
	 */
	@Override
	public <T> void addBean(final T bean) throws Exception {
		addBean(beanNameConverter.convert(bean.getClass()), bean);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.core.ApplicationContext#addBean(java.lang.String, java.lang.Object)
	 */
	@Override
	public <T> void addBean(final String name, final T bean) throws Exception {
		beanMap.put(name, bean);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.core.ApplicationContext#getBean(java.lang.Class)
	 */
	@Override
	public <T> T getBean(final Class<T> clz) throws Exception {
		return getBean(beanNameConverter.convert(clz));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.core.ApplicationContext#getBean(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getBean(final String name) throws Exception {
		return (T) beanMap.get(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.core.ApplicationContext#getBeanMap()
	 */
	@Override
	public Map<String, Object> getBeans() {
		return beanMap;
	}

}
